﻿using mediaControl.ViewModels;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;

namespace mediaControl.Views
{
    /// <summary>
    /// monitor.xaml 的交互逻辑
    /// </summary>
    public partial class Monitor4 : UserControl
    {
        public Monitor4()
        {
            InitializeComponent();
            Loaded += UserControlLoaded;
        }

        private void UserControlLoaded(object sender, RoutedEventArgs e)
        {
            if (DataContext is Monitor4ViewModel viewModel)
            {
                viewModel.ImagePlayer = monitorImagePlayer4;
                viewModel.VideoPlayer = monitorVideoPlayer4;
                viewModel.WebBrowser = monitorWeb4;// 将UserControl自身传递给ViewModel
                viewModel.TranslateTransform = translation4;
                viewModel.ScaleTransform = scaleTransform4;
                viewModel.TranslateTransformNext = translationNext4;
                viewModel.ImagePlayerNext = monitorImagePlayerNext4;
                viewModel.ScaleTransformNext = scaleTransformNext4;
                viewModel.ImageTran = mainpImage4;
            }
        }

        public void SuppressScriptErrors(WebBrowser wb, bool hide)
        {
            FieldInfo fiComWebBrowser = typeof(WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);
            if (fiComWebBrowser == null) return;

            object objComWebBrowser = fiComWebBrowser.GetValue(wb);
            if (objComWebBrowser == null) return;

            objComWebBrowser.GetType().InvokeMember("Silent", BindingFlags.SetProperty, null, objComWebBrowser, new object[] { hide });
        }

        private void web_Navigating(object sender, System.Windows.Navigation.NavigatingCancelEventArgs e)
        {
            SuppressScriptErrors(sender as WebBrowser, true);
        }

        private void monitorVideoPlayer4_MediaOpened(object sender, RoutedEventArgs e)
        {
            monitorImagePlayer4.Visibility = Visibility.Hidden;
            monitorWeb4.Visibility = Visibility.Hidden;
            if (null != monitorWeb4 && monitorWeb4.Children.Count > 0)
            {
                monitorWeb4.Children.RemoveAt(monitorWeb4.Children.Count - 1);
            }
            monitorImagePlayer4.Source = null;
            monitorImagePlayerNext4.Visibility = Visibility.Hidden;
            monitorImagePlayerNext4.Source = null;
            mainpImage4.Content = null;
            mainpImage4.Visibility = Visibility.Hidden;
        }
        private void UserControl_Drop(object sender, DragEventArgs e)
        {
            // 获取拖放的文件列表
            string[] files = (string[])e.Data.GetData("FileDrop");

            // 播放第一个文件，假设我们只处理文件
            if (files != null && files.Length > 0)
            {

                string extension = Path.GetExtension(files[0]).ToLowerInvariant();

                if (extension == ".jpg" || extension == ".jpeg" || extension == ".png" || extension == ".gif" || extension == ".bmp")
                {
                    monitorImagePlayer4.Visibility = Visibility.Visible;
                    monitorImagePlayer4.Source = (ImageSource)new ImageSourceConverter().ConvertFromString(files[0]);
                    monitorVideoPlayer4.Visibility = Visibility.Hidden;
                    monitorVideoPlayer4.Source = null;
                    monitorVideoPlayer4.Stop();
                }
                else if (extension == ".mp4" || extension == ".mov" || extension == ".wmv" || extension == ".avi" || extension == ".mkv")
                {
                    monitorVideoPlayer4.Visibility = Visibility.Visible;
                    monitorVideoPlayer4.Source = new Uri(files[0]);
                    monitorVideoPlayer4.Play();
                    monitorImagePlayer4.Source = null;
                    monitorImagePlayer4.Visibility = Visibility.Hidden;
                }


                // 将MediaElement添加到界面上的某个位置
                //data.Children.Add(mediaElement); // 或者其他布局控件的Children集合中
            }
        }

        private void UserControl_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effects = DragDropEffects.Link;
            }
            else
            {
                e.Effects = DragDropEffects.None;
            }
        }
    }
}
